﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using ProducerMQ.Models;

namespace ProducerMQ.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        private readonly RabbitMQClient _rabbitMQClient;

        public HomeController(ILogger<HomeController> logger, RabbitMQClient rabbitMQClient)
        {
            _logger = logger;
            _rabbitMQClient = rabbitMQClient;
        }

        public IActionResult Index()
        {
            return View();
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }

        /// <summary>
        /// 向队列中发送消息 
        /// </summary>
        /// <param name="msgView"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult SendMsg([FromBody]MsgViewModel msgView)
        {
            if (msgView == null || string.IsNullOrWhiteSpace(msgView.content))
            {
                return Json(new { flag = 1, msg = "value is null" });
            }
            
            _rabbitMQClient.PushMessage("done.task3", msgView.content);

            return Json(new { flag = 0 });
        }

    }
}
